﻿using Supermarket;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XLSupermarket.Financial
{
    public partial class FinancialForm : Form
    {
        public FinancialForm()
        {
            InitializeComponent();
        }

        private void Financial_Load(object sender, EventArgs e)
        {
            try { 
            The_financial_statementspanel.Show();
            Order_querypanel.Hide();
            Set_panel.Hide();
            //获取从主页面获取的图片
            string userName = LoginForm.UserName_Value;//账号
            string sql = "select Src from EP_Number where UserName='" + userName + "'";
            DataTable sdr = DBHelper.FillData(sql);
            string path = sdr.Rows[0]["Src"].ToString();
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            Image img = Image.FromStream(fs);
            PB_Photo.Image = img;
            //获取从主页面获取的员工姓名
            string sql1 = "select EP_Name from EP_Number en,EP_information ei where en.EP_ID = ei.EP_ID and en.UserName = '" + userName + "'";
            DataTable sdr1 = DBHelper.FillData(sql1);
            string name = sdr1.Rows[0][0].ToString();
            this.label2.Text = name;

            string sql2 = "select DATEDIFF(DAY,GETDATE(),DATEADD(DAY,QualityGuaranteePeriod,DateOfManufacture)) 剩余日期 from Goods_Warehousing gw order by 剩余日期 asc";
            DataTable dt = DBHelper.FillData(sql2);
            int count = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (int.Parse(dt.Rows[i][0].ToString()) <= 10)
                {
                    count++;
                }
            }
            if (count > 0)
            {
                MessageBox.Show("有"+count.ToString()+"个商品就快过期了，请尽快处理","提醒",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }



            string sql3 = "select Staff_Name from EP_Number en,EP_information ei where en.EP_ID = ei.EP_ID and en.UserName = '" + userName + "'";
            DataTable sdr3 = DBHelper.FillData(sql3);
            if (sdr3.Rows[0][0].ToString() == "店长")
            {
                this.button3.Show();
            }
            else
            {
                this.button3.Hide();
            }
            }
            catch (Exception ex)
            {
                MessageBox.Show("出现错误：" + ex.Message);
            }
        }

        private void Prompt_information_toolStripButton_Click(object sender, EventArgs e)
        {
            Prompt_information.Prompt_information prompt_information = new Prompt_information.Prompt_information();
            prompt_information.ShowDialog();
        }

        private void Summary_statisticsbutton_Click(object sender, EventArgs e)
        {
            The_statistical_summary.The_statistical_summaryForm the_Statistical_SummaryForm = new The_statistical_summary.The_statistical_summaryForm();
            the_Statistical_SummaryForm.ShowDialog();
        }

        private void Income_detailsbutton_Click(object sender, EventArgs e)
        {
            Income_details.Income_detailsForm income_DetailsForm = new Income_details.Income_detailsForm();
            income_DetailsForm.ShowDialog();
        }

        private void Spending_recordbutton_Click(object sender, EventArgs e)
        {
            Spending_record.Spending_recordForm spending_RecordForm = new Spending_record.Spending_recordForm();
            spending_RecordForm.ShowDialog();
        }

        private void Wagesbutton_Click(object sender, EventArgs e)
        {
            Wages.WagesForm1 wagesForm = new Wages.WagesForm1();
            wagesForm.ShowDialog();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            The_financial_statementspanel.Show();
            Order_querypanel.Hide();
            Set_panel.Hide();
        }

        private void Order_querybutton_Click(object sender, EventArgs e)
        {
            The_financial_statementspanel.Hide();
            Order_querypanel.Show();
            Set_panel.Hide();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            The_financial_statementspanel.Hide();
            Order_querypanel.Hide();
            Set_panel.Show();
        }

        private void FinancialForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = MessageBox.Show("你确定要关闭嘛?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
            if (result == DialogResult.No)
            {
                e.Cancel = true;
            }
            else
            {
                System.Environment.Exit(0);
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            LoginForm loginForm = new LoginForm();
            DialogResult result = MessageBox.Show("你确定要返回嘛?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                this.Hide();
                loginForm.Show();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try { 
            string Order_Number = this.textBox2.Text;
            string Time_Of_Sale = this.dateTimePicker1.Value.ToString("yyyy-MM-dd");
            string sql = string.Format("select so.Order_Number 订单编号,ei.EP_Name 员工姓名,so.Cash_Register_Method 支付方式,so.VIP_ID 会员编号,so.Add_Score 获得积分,so.Receivables 应收账款,so.Paid_in_Amount 支付金额,so.Give_Change 找零,so.Time_Of_Sale 销售日期,so.Sales_Number 销售数量 from Sales_Order so,EP_information ei where so.EP_ID=ei.EP_ID and Order_Number like '%{0}%' and Time_Of_Sale like '%{1}%'", Order_Number,Time_Of_Sale);
            this.dataGridView1.DataSource = DBHelper.FillData(sql);
            }
            catch (Exception ex)
            {
                MessageBox.Show("出现错误：" + ex.Message);
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            Account.Information.seletInformationForm ais = new Account.Information.seletInformationForm();
            ais.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Account.ChangePassword.UpPwd acp = new Account.ChangePassword.UpPwd();
            acp.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MainInterface.MainForm mm = new MainInterface.MainForm();
            this.Hide();
            mm.Show();
        }
    }
}
